package com.btpj.lib_base.widgets

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.btpj.lib_base.R


class GenderAgeView(context: Context, attrs: AttributeSet, def: Int) : LinearLayout(context, attrs, def) {
    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
    private lateinit var mTvAge: TextView
    private lateinit var mIvGender: ImageView
    private var mAge = 0
    private var mGender = 0

    init {
        LayoutInflater.from(context).inflate(R.layout.include_gender_age, this, true)
        gravity = Gravity.CENTER
        orientation = HORIZONTAL
        mTvAge = findViewById(R.id.tvAge)
        mIvGender = findViewById(R.id.ivGender)
        val attributes: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.GenderAgeView)
        val age = attributes.getInt(R.styleable.GenderAgeView_gav_age, 0)
        val gender = attributes.getInt(R.styleable.GenderAgeView_gav_gender, 0)
        setAgeGender(age, gender)
        attributes.recycle()
    }

    fun setAgeGender(age: Int, gender: Int) {
        if (age < 18) {
            mTvAge.visibility = GONE
        } else {
            mTvAge.visibility = VISIBLE
        }
        mTvAge.text = if (age == 0) "" else age.toString()
        mAge = age
        mGender = gender
        if (gender != 2 && mAge != -1) {
            visibility = VISIBLE
            setBackgroundResource(if (gender == 1) R.drawable.shape_soild_7dp_d1effb else R.drawable.shape_soild_7dp_ffefed)
            mIvGender.setImageResource(if (gender == 1) R.drawable.ic_mind_man else R.drawable.ic_mind_woman)
            mTvAge.setTextColor(if (gender == 1) ContextCompat.getColor(context,
                R.color.c_32C5FF) else ContextCompat.getColor(
                context, R.color.c_FFB3AA))
        } else {
            visibility = GONE
        }
    }

    fun setGender(gender: Int) {
        mGender = gender
        if (gender != 0) {
            visibility = VISIBLE
            setBackgroundResource(if (gender == 1) R.drawable.shape_soild_7dp_d1effb else R.drawable.shape_soild_7dp_ffefed)
            mIvGender.setImageResource(if (gender == 1) R.drawable.ic_mind_man else R.drawable.ic_mind_woman)
            mTvAge.setTextColor(if (gender == 1) ContextCompat.getColor(context,
                R.color.c_32C5FF) else ContextCompat.getColor(
                context, R.color.c_FFB3AA))
        } else {
            visibility = GONE
        }
    }

    fun setGen(gender: Int) {
        mGender = gender
        if (gender != 0) {
            visibility = VISIBLE
            setBackgroundResource(if (gender == 1) R.drawable.shape_soild_7dp_d1effb else R.drawable.shape_soild_7dp_ffefed)
            mIvGender.setImageResource(if (gender == 1) R.drawable.ic_mind_man else R.drawable.ic_mind_woman)
            mTvAge.visibility = GONE
        }
    }

    fun setAge(age: Int) {
        mAge = age
        mTvAge.text = age.toString()
    }
}